#!/bin/sh

# wait to start web and run from goahead code
. /etc/scripts/web_wait.sh
web_wait

# get params
. /etc/scripts/global.sh

LOG="logger -t wan"

start() {
 $LOG "Start WAN config"
    get_param

    # preconfigure WAN interface
    wan_if_preconf

    ####################################################################################
    # Start config wan and start configure daemon depend by current mode
    ####################################################################################

    # enable wan interface
    ip link set "$wan_if" up

    if [ "$wanConnectionMode" = "STATIC" ] || [ "$OperationMode" = "0" ] || [ "$ApCliBridgeOnly" = "1" ]; then
	if [ "$OperationMode" != "0" ] && [ "$ApCliBridgeOnly" != "1" ]; then
	    # lan and wan ip should not be the same except in bridge mode
	    if [ "$wan_ipaddr" = "$lan_ipaddr" ]; then
		$LOG "ERROR: WAN's IP address is set identical to LAN. Need change LAN_IP adress!!!"
		exit 0
	    fi
	    # wan interface up and default gateway
	    ifconfig "$wan_if" "$wan_ipaddr" netmask "$wan_netmask"
	    # always treat bridge mode having static wan connection
	    # route mode
	    if [ "$wan_gateway" != "" ] && [ "$wan_gateway" != "0.0.0.0" ]; then
		ip route replace default via $wan_gateway
	    fi
	    # add routes configured in web
	    if [ -f /etc/routes_replace ]; then
		$LOG "Add user routes."
		/etc/routes_replace replace "$phys_lan_if" "$wan_if"
	    fi
	    # Add route to multicast subnet
	    if [ "$igmpEnabled" = "1" -o "$UDPXYMode" != "0" ] || [ "$xupnpd" = "1" ]; then
		$LOG "Add route to multicast subnet."
		ip route replace "$mcast_net" dev "$wan_if"
	    fi
	    # send Cisco Discovery request
	    if [ -f /bin/cdp-send ] && [ -f /etc/scripts/config-cdp.sh ]; then
		config-cdp.sh &
	    fi
	else
	    # wan interface up and default gateway
	    ifconfig br0 $lan_ipaddr netmask $lan_netmask
	    # if bridge mode dgw on dev br0 set
	    if [ "$wan_gateway" != "" ] && [ "$wan_gateway" != "0.0.0.0" ]; then
    		ip route replace default dev br0 via $wan_gateway
	    else
    		ip route replace default dev br0
	    fi
        fi
    elif [ "$wanConnectionMode" = "DHCP" ] && [ "$vpnPurePPPOE" != "1" ]; then
	# reload global
	. /etc/scripts/global.sh

	# get udhcp param
	udhcpc_opts

	$LOG "Start DHCP client."
	(sleep 2 && udhcpc $UDHCPCOPTS > /dev/null 2>&1) &
    elif [ "$wanConnectionMode" = "ZERO" ] || [ "$vpnPurePPPOE" = "1" ]; then
	# reload global
	. /etc/scripts/global.sh

	$LOG "Call zeroconf for get wan ip address."
	zero_conf
    else
	$LOG "Unknown mode. Please reset device."
    fi
}

wan_if_preconf() {
    # if not bridge wan up
    if [ "$OperationMode" != "0" ] && [ "$ApCliBridgeOnly" != "1" ]; then
	# drop adress from wan if pure pppoe enabled
	if [ "$vpnEnabled" = "on" ] && [ "$vpnType" = "0" ] && [ "$vpnPurePPPOE" = "1" ]; then
	    $LOG "PURE PPPOE Mode - NOT start dhcpc or config wan!"
	    ip addr flush dev "$wan_if"
	fi
	# set manual mtu to wan if set
	if [ "$wan_manual_mtu" != "0" ] && [ "$wan_manual_mtu" != "" ]; then
	    $LOG "Set manual MTU to WAN $wan_manual_mtu"
	    ip link set mtu $wan_manual_mtu dev "$wan_if"
	fi
	# enable/disable forward for wan
	if [ "$vpnEnabled" = "on" ] && [ "$vpnType" = "0" ] && [ "$vpnPurePPPOE" = "1" ]; then
	    # no need forward for phys wan
	    echo 0 > "/proc/sys/net/ipv4/conf/$wan_if/forwarding"
	else
	    echo 1 > "/proc/sys/net/ipv4/conf/$wan_if/forwarding"
	fi
    fi
}

get_param() {
    # get parametrs
    eval `nvram_buf_get 2860 wan_ipaddr wan_netmask wan_gateway wan_manual_mtu lan_ipaddr lan_netmask HostName xupnpd`
}

stop() {
    # stop dhcp and zeroconf client
    killall -q udhcpc
    killall -q zcip
    killall -q -SIGKILL udhcpc
    killall -q -SIGKILL zcip
    # prevent infinitive route loop in kernel code
    if [ "$OperationMode" != "0" ] && [ "$ApCliBridgeOnly" != "1" ] && [ "$vpnEnabled" = "on" ] && [ "$vpnType" != "0" ]; then
	ip route flush dev "$vpn_if" > /dev/null 2>&1
    fi
    # in gw/hotspot modes down wlan if not bridge and not ethconvert
    if [ "$OperationMode" = "1" ] || [ "$OperationMode" = "4" -a "$ApCliBridgeOnly" != "1" ]; then
	# down wan iface
	ip link set "$wan_if" down > /dev/null 2>&1
    fi
    # clear conntrack and routes tables/caches
    flush_net_caches
}

case "$1" in
	start)
	    start
	    ;;

	stop)
	    stop
	    ;;

	restart)
	    stop
	    start
	    ;;

	*)
	    echo $"Usage: $0 {start|stop|restart}"
	    exit 1
esac
